//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		$('#goodsDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#goodsDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				bar_code:bar_code
			}
		});
	})
	//更新行数据
	function updateActions(value){
		$.post(confirmEditGoodsUrl,{id:value.id,goods_cate:value.goods_cate},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#goodsDatagrid').datagrid('validateRow', editIndex)){
			$('#goodsDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#goodsDatagrid').datagrid('selectRow', index);
				$('#goodsDatagrid').datagrid('endEdit', editIndex);
				$('#goodsDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#goodsDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};

	$('#goodsDatagrid').datagrid({
		//url:getgoodsUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getgoodslistUrl,
		title:'商品管理',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'manua_id',title:'账册编号',sortable:true},
		{field:'goods_name',title:'商品名称'},
		// {field:'goods_cate',title:'商品类型',
			// formatter:function(val,rowData,rowIndex){
		//         if(val==1){
		//             return '一般商品';
				// }
				// if(val==2){
		//             return '原料';
				// }
				// if(val==3){
		//             return '辅料';
				// }
		//     },
			// editor:{
				// type:'combobox',
				// options:{
					// valueField:'id',
					// textField:'name',
					// method:'get',
					// url:goodscateListUrl,
					// required:true,
					// validType:['selected[]']
				// }
			// }
		// },
		{field:'bar_code',title:'条形码'},
		{field:'goods_spec',title:'规格'},
		{field:'unit1',title:'第一单位',
			formatter:function(value,row,index){
				var json = unit_lists;  //创建一个json
				var len = json.length;
				for (var i =0;i<len;i++){
					if(value==json[i].unit_no){
						return json[i].unit_name;
					}
				}
			}
		},
		{field:'unit2',title:'第二单位',
			formatter:function(value,row,index){
				var json = unit_lists;  //创建一个json
				var len = json.length;
				for (var i =0;i<len;i++){
					if(value==json[i].unit_no){
						return json[i].unit_name;
					}
				}
			}
		},
		{field:'goods_price',title:'入库平均价格',
			formatter:function(val,rowData,rowIndex){
		        if(val!=null){
		            return Math.round(val*100)/100;
				}
		    }
		},
		{field:'amount',title:'总数量'},
		{field:'storage_amount',title:'库存数量'},
		{field:'freez_amount',title:'冻结数量'},
		{field:'conforming_p',title:'合格品',sortable:true},
		{field:'nonconforming_p',title:'残次品',sortable:true},
		{field:'scrap_one',title:'报废品',sortable:true},
		{field:'bad_product',title:'不良品',sortable:true},
		// {field:'push_time',title:'推送时间',sortable:true},
		// {field:'push_times',title:'推送次数',sortable:true},
		{field:'info_count',title:'批次数量'}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
goodsObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#goodsDatagrid').datagrid('validateRow', editIndex)){
			$('#goodsDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#goodsDatagrid').datagrid('acceptChanges');
		}
	},
	editBar:function(){
		var goodsRow=$('#goodsDatagrid').datagrid('getSelections');
        if (goodsRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(goodsRow.length==1){            	
        	$('#goodsEditDialog').dialog('open').dialog('setTitle','编辑商品');
            $('#goodsEditForm').form('load',goodsRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	infoBar:function(){//查看商品详细
		var goodsRow=$('#goodsDatagrid').datagrid('getSelections');
        if (goodsRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(goodsRow.length==1){
			// console.log(goodsRow[0]['id']);
			var id=goodsRow[0]['id'];
			var inbound_status=1;
        	window.parent.showTab(goodsbatchlistUrl,'商品批次明细',id,inbound_status);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	save:function(url){
		$('#goodsEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#goodsEditDialog').dialog('close');
                    });
                }else{
                    $('#goodsEditDialog').dialog('close');      
                    $('#goodsDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#goodsDatagrid').datagrid({ queryParams:''});
	},
	reloadBar:function(){
		$('#goodsDatagrid').datagrid('reload');
	},
	pushBar:function(){
		var goodsRow=$('#goodsDatagrid').datagrid('getSelections');
		var text='确定推送吗！';
        if (goodsRow.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<goodsRow.length;i++){
                		ids.push(goodsRow[i].id);
						// if(goodsRow[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+goodsRow[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(goodspushUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	excelBar:function(){//导出excel
		var thisform=$('#goodsSearchForm');
		// console.log(thisform);
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		url=getgoodslistUrl+"&excel=1&rows=6000";
		if(goods_no!=''){
			url=url+"&goods_no="+goods_no;
		}
		if(goods_name!=''){
			url=url+"&goods_name="+goods_name;
		}
		if(bar_code!=''){
			url=url+"&bar_code="+bar_code;
		}
		window.location.href=url;
	}

}